﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BEGDC.PreContract.BLL;

namespace PreContractWeb.Administration
{
    public partial class LogViewer : System.Web.UI.Page
    {

        private BEGDC.PreContract.BLL.Authentication.PCIdentity user;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.btn_search.Attributes["autopostback"]==null)
                this.btn_search.Attributes.Add("autopostback", "false");

            BEGDC.PreContract.BLL.IUserRole usrRole = (BEGDC.PreContract.BLL.IUserRole)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_UserRole_Service];
            user = new BEGDC.PreContract.BLL.Authentication.PCIdentity(HttpContext.Current.User.Identity);

            if (!usrRole.IsRoleAdmin(user.EmployeeId))
            {
                this.divError.InnerHtml = AlertMessage.NoRoleAdminPermission;
                this.divContent.Visible = false;
                this.divError.Visible = true;
            }
            LoadStrings();

            if (!IsPostBack)
            {
                
                //this.lb_opmd.Text = BEGDC.MIS.Resource.OperationLog.OperationModule;

                BindDropDownList();

                //this.txtstartdate.Text = DateTime.Today.ToString("yyyy-MM-dd");

                //this.txtenddate.Text = DateTime.Today.AddDays(1).ToString("yyyy-MM-dd");

                //this.btn_search.
                this.btn_search.Attributes.Add("onClick", "CheckInput();");

                //BindGridView();
            }

            LoadStrings();
        }

        private void LoadStrings()
        {
            this.btn_search.Text = "查询 (Search)";
        }

        private void BindDropDownList()
        {

            Type operationType = typeof(BEGDC.PreContract.BLL.OperationLog.OperationType);

            foreach (string operation in Enum.GetNames(operationType))
                this.drpmodule.Items.Add(new ListItem(operation));


            this.drpmodule.Items.Insert(0, new ListItem("ALL", ""));
        }

        private void BindGridView()
        {
            BEGDC.PreContract.BLL.IOperationLog logservice = (BEGDC.PreContract.BLL.IOperationLog)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_OperationLog_Service];

            string filter = " 1 = 1 ";

            if (this.drpmodule.SelectedValue != string.Empty)
            {
                filter += " AND OperationType = '" + this.drpmodule.SelectedValue + "'";
            }
            if (this.txtename.Text.Trim() != string.Empty)
            {
                filter += " AND EmployeeName Like '%" + BaseFunction.SafeSQL(this.txtename.Text.Trim()) + "%'";
            }

            //modified Joan 2008-10-09 

            if (this.txtstartdate.Text.Trim() != string.Empty)
                filter += " AND OperationDate>='" + this.txtstartdate.Text.Trim() + "'";
            if (this.txtenddate.Text.Trim() != string.Empty)
                filter += " AND DATEADD(dd,-1,OperationDate)<='" + this.txtenddate.Text.Trim() + "'";



            //if (this.txtstartdate.Text.Trim() != string.Empty && this.txtenddate.Text.Trim() != string.Empty)
            //{
            //    filter += " AND OperationDate Between '" + this.txtstartdate.Text.Trim() + "' AND '" + this.txtenddate.Text.Trim() + "'";
            //}
            //end
            System.Data.DataTable dt;

            if (rbtAll.Checked)
            {
                dt = logservice.GetOperationLogs(filter, true);
            }
            else
            {
                dt = logservice.GetOperationLogs(filter, false);
            }

            this.pgoperationlog.Visible = true;
            this.logPanel.Visible = true;
            dt.DefaultView.Sort = "OperationDate DESC";
            this.pgoperationlog.BindtoGridView(this.logdataGridView, dt.DefaultView);

            if (dt.Rows.Count == 0)
                this.pgoperationlog.Visible = false;

        }

        protected void btn_search_Click(object sender, EventArgs e)
        {
            //if (this.txtstartdate.Text != string.Empty && this.txtenddate.Text == string.Empty)
            //{

            //    this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('结束时间不能为空.')</script>",false); 
            //    this.txtenddate.Focus();
            //    return;
            //}

            //if (this.txtstartdate.Text == string.Empty && this.txtenddate.Text != string.Empty)
            //{
            //    this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('开始时间不能为空.');</script>", false);
            //    this.txtstartdate.Focus();
            //    return;
            //}

            if (this.txtstartdate.Text != string.Empty && this.txtenddate.Text != string.Empty)
            {
                if (Convert.ToDateTime(this.txtstartdate.Text) > Convert.ToDateTime(this.txtenddate.Text))
                {
                    this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('开始时间不能大于结束时间.');</script>", false);

                    this.txtenddate.Focus();
                    return;
                }
            }

            this.pgoperationlog.GVCurrentPageIndex = 1;

            BindGridView();

        }


    }
}
